﻿using System;
using System.Collections.Generic;
using System.Web;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Common;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Filters;
using AMS.DataAccess.Filter;
using AMS.DataAccess.Trademark.BO.Enums;

/// <summary>
/// Summary description for TaskGenerator
/// </summary>
public class TaskGenerator
{
    public TaskGenerator(CallContext context, RecvFileInfo rfInfo)
    {
        this.context = context;
        this.rfInfo = rfInfo;
        if (rfInfo.CreateDateTime == DateTime.MinValue)
        {
            rfInfo.CreateDateTime = DateTime.Now;
        }
    }

    CallContext context;
    RecvFileInfo rfInfo;
    public CallContext Context
    {
        get { return context; }
    }
    public RecvFileInfo RFInfo
    {
        get { return rfInfo; }
    }

    private RFTaskInfo currentRFTask;
    private TaskInfo currentTask;

    public void GenerateTask(int assignedUserID)
    {
        IRecvFileManager rfMgr = ManagerFactory.CreateRecvFileManager();
        RFTaskFilter filter = new RFTaskFilter();
        filter.RFIndex = new StringFilterField(rfInfo.RFIndex);
        List<RFTaskInfo> rftasklist = rfMgr.GetRFTask(Context, filter);
        if (rftasklist.Count > 0)
        {
            List<TaskInfo> taskList = new List<TaskInfo>();
            foreach (RFTaskInfo rftask in rftasklist)
            {
                TaskInfo task = new TaskInfo();
                currentTask = task;
                currentRFTask = rftask;

                task.TaskName = rftask.TaskName;
                task.TaskIndex = rftask.TaskIndex;
                task.Priority = TaskPriority.Normal;
                task.RefRFID = rfInfo.AID;
                if (assignedUserID != 0)
                {
                    task.OwnerID = assignedUserID;
                    task.Status = TaskStatus.NewTask;
                }

                task.StartDateTime = DateTime.Now;
                task.CreateUserID = context.UserID;
                CaculateTaskDeadline(task, null);
                taskList.Add(task);
            }

            if (taskList.Count > 0)
            {
                ITaskManager taskMgr = ManagerFactory.CreateTaskManager();
                taskMgr.BatchAddTasks(Context, taskList);
            }

            rfMgr.SetRFAlreadyGenerateTaskFlag(Context, rfInfo.AID);
        }
    }

    private void CaculateTaskDeadline(TaskInfo task, CaseBasicInfo caseInfo)
    {
        // TODO: 完成实现
        task.InnerDeadlineDate = rfInfo.CreateDateTime.AddMonths(currentRFTask.iAddMonth).AddDays(currentRFTask.iAddDay);
        task.OuterDeadlineDate = rfInfo.CreateDateTime.AddMonths(currentRFTask.oAddMonth).AddDays(currentRFTask.oAddDay);
    }
}
